package com.rd.student.ruanjian.class3.gaochenxi.LianXi3.NumberFour;

import com.rd.student.ruanjian.class3.gaochenxi.LianXi3.NumberSeven.NumberSeven;

public class NumberFourThree {
   //使用wait和notify()
   int[] ai = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   String[] ac = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
   private int flag = 1;
   private int i = 0;

   private int j = 0;

   public synchronized void print1() {
      while (flag != 1) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 2;
      notifyAll();
   }

   public synchronized void print2() {

      while (flag != 2) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 3;
      notifyAll();
   }

   public synchronized void print3() {

      while (flag != 3) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 4;
      notifyAll();
   }

   public synchronized void print4() {

      while (flag != 4) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 5;
      notifyAll();
   }

   public synchronized void print5() {

      while (flag != 5) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 6;
      notifyAll();
   }

   public synchronized void print6() {

      while (flag != 6) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 7;
      notifyAll();
   }

   public synchronized void print7() {

      while (flag != 7) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 8;
      notifyAll();
   }

   public synchronized void print8() {

      while (flag != 8) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 9;
      notifyAll();
   }

   public synchronized void print9() {

      while (flag != 9) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ai.length) {
         System.out.print(ac[i] + "正在通过山洞,");
         i++;
         System.out.println("正在通知" + ac[i]);
      } else {
         return;
      }
      flag = 10;
      notifyAll();
   }

   public synchronized void print10() {

      while (flag != 10) {
         try {
            wait();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
      }
      if (i < ac.length) {
         System.out.print(ac[i] + "正在通过山洞");
         System.out.println();
         System.out.println("所有人已通过山洞");
         return;
      }
   }


   public static void main(String[] args) {
      NumberFourThree n = new NumberFourThree();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print1();
         }
      }).start();

      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print2();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print3();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print4();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print5();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print6();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print7();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print8();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print9();
         }
      }).start();
      new Thread(new Runnable() {
         @Override
         public void run() {
            n.print10();
         }
      }).start();
   }
}
